<?php
namespace Core;

use Phalcon\Filter as PhalconFilter;
use Phalcon\Di;

class Filter extends PhalconFilter{

    public function __construct()
    {
        $filters = Di::getDefault()->getShared('config')->cache('filter');
        foreach ($filters as $filterName => $filter){
            $this->add($filterName,$filter['function']);
        }
    }

    protected function _sanitize($value, $filter)
    {
        if(strstr($filter,'(')){
            $funcInfo = explode('(',trim($filter,') '));
            $funcParams = explode(',',$funcInfo[1]);
            $funcName = $funcInfo[0];
        }else{
            $funcParams = [];
            $funcName = $filter;
        }
        if(function_exists($funcName)){
            array_unshift($funcParams,$value);
            return call_user_func_array($funcName,$funcParams);
        }
        return parent::_sanitize($value, $filter);
    }
}